package com.example.communitypro.controller;

import com.example.communitypro.pojo.*;
import com.example.communitypro.service.NotificationService;
import com.example.communitypro.service.PostService;
import com.example.communitypro.service.UserInformationService;
import com.example.communitypro.service.UserLikesService;
import com.example.communitypro.service.impl.UserLikesServiceImpl;
import com.example.communitypro.util.result.Result;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/userLikes")
public class UserLikesController {

    @Autowired
    private UserLikesService userLikesService;

    @Autowired
    private UserInformationService userInformationService;
    @Autowired
    private PostService postService;

    @Autowired
    private NotificationService notificationService;

    @ResponseBody
    @RequestMapping(value = "/addUserLikes")
    @CrossOrigin
    public Result addUserLikes(UserLikes record){
        Notification notification1 = new Notification();
        notification1.setNotificationDate(new Date());

        notification1.setNotificationType(1);
        notification1.setPostNo(record.getLikeObjectNo());
        notification1.setUserNo(record.getUserNo());
        notificationService.addNotification(notification1);
        return userLikesService.addUserLikes(record);
    }

    @ResponseBody
    @RequestMapping(value = "/dropUserLikes")
    @CrossOrigin
    public Result dropUserLikes(Integer likeId){
        return userLikesService.dropUserLikes(likeId);
    }

    @ResponseBody
    @RequestMapping(value = "/modifyUserLikes")
    @CrossOrigin
    public Result modifyUserLikes(UserLikes record){
        return userLikesService.modifyUserLikes(record);
    }

    @ResponseBody
    @RequestMapping(value = "/queryUserLikes")
    @CrossOrigin
    public Result queryUserLikes(Integer likeId){
        return userLikesService.queryUserLikes(likeId);
    }

    @ResponseBody
    @RequestMapping(value = "/queryCommunityLike")
    @CrossOrigin
    public Result queryCommunityLike(Integer user_no){
        return userLikesService.queryCommunityLike(user_no);
    }

    @ResponseBody
    @RequestMapping(value = "/queryPostLike")
    @CrossOrigin
    public Result queryPostLike(Integer user_no){
        return userLikesService.queryPostLike(user_no);
    }

    @ResponseBody
    @RequestMapping(value = "/queryCommentLike")
    @CrossOrigin
    public Result queryCommentLike(Integer user_no){
        return userLikesService.queryCommentLike(user_no);
    }

    @ResponseBody
    @RequestMapping(value = "/UserLikes")
    @CrossOrigin
    public Result UserLikes(UserLikes record){
        Notification notification1 = new Notification();
        notification1.setNotificationDate(new Date());

        if(record.getLikeObjectType()==1){
            notification1.setNotificationType(1);
        }else if(record.getLikeObjectType()==2){
            notification1.setNotificationType(3);
        }
        notification1.setPostNo(record.getLikeObjectNo());
        notification1.setUserNo(record.getUserNo());
        notificationService.addNotification(notification1);
        return userLikesService.UserLikes(record);
    }

    @ResponseBody
    @RequestMapping(value = "/findUserToOther")
    @CrossOrigin
    public List<String> findUserToOther(Integer user_no){
        List<String> res = new ArrayList<>();
        List<Integer> userToOther = userLikesService.findUserToOther(user_no);
        for (Integer integer : userToOther) {

            Post post = (Post) postService.queryPost(integer).getData().get("post");
            Result result = userInformationService.queryUserInformation(post.getUserNo());


            UserInformation userInformation = (UserInformation) result.getData().get("userInformation");
            res.add("你对"+userInformation.getUserAccount()+"的贴子点赞啦");
        }
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/findOtherToUser")
    @CrossOrigin
    public List<String> findOtherToUser(HttpServletRequest request,Integer userNo){
        List<String> res = new ArrayList<>();
//        List<Integer> userToOther = userLikesService.findUserToOther((Integer) request.getSession().getAttribute("userNo"));
//        Integer userNo = (Integer) request.getSession().getAttribute("userNo");

        List<Post> posts = (List<Post>) postService.queryUserPost(userNo).getData().get("posts");

        for (Post post : posts) {
            List<Integer> userByPost = userLikesService.findUserByPost(post.getPostNo());
            for (Integer integer : userByPost) {
                UserInformation userInformation = (UserInformation) userInformationService.queryUserInformation(integer).getData().get("userInformation");
                res.add("你的标题为--"+post.getPostTitle()+"--的贴子被"+userInformation.getUserAccount()+"点赞啦");
            }
        }
        return res;
    }

}
